﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Seven.Models;
using WMS.Seven.Qi.Dto;

namespace WMS.Seven.Qi
{
    /// <summary>
    /// 产品
    /// </summary>
    public class ProductService : ApplicationService, IProductService
    {
        private readonly IRepository<Products, int> _productRepository;
        public ProductService(IRepository<Products, int> productRepository)
        {
            _productRepository = productRepository;
        }
        /// <summary>
        /// 获取产品
        /// </summary>
        /// <param name="code"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<object> GetProduct(RequestProductDTO productDTO)
        {
            var productList = await _productRepository.GetQueryableAsync();
            productList = productList.WhereIf(!string.IsNullOrEmpty(productDTO.ProductCode), x => x.ProductCode.Contains(productDTO.ProductCode));
            productList = productList.WhereIf(!string.IsNullOrEmpty(productDTO.ProductName), x => x.ProductName.Contains(productDTO.ProductName));
            var count = productList.Count();
            productList = productList.OrderBy(x => x.Id).Skip(((productDTO.Page - 1) ?? 0) * (productDTO.Limit ?? 10)).Take(productDTO.Limit ?? 10);
            var result = new ResponseModel
            {
                code = "",
                count = count,
                data = productList.ToList(),
                msg = "",
            };
            return result;
        }

        /// <summary>
        /// 获取销售产品
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<object> GetProductByWhere(string code)
        {
            var product = await _productRepository.GetListAsync(m => m.ProductCode == code);
            return product;
        }

    }
}
